/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>

#include "urls.h"

void VersionURLS::load(const std::string filename) {
	boost::filesystem::path file(filename); /// URLS file handler.
	std::string line; /// Line in urls file.
	std::vector<std::string> parts; /// Version and urls parts of the line.

	if (boost::filesystem::exists(file)) {
		std::ifstream in(file.string().c_str());
		while (getline(in, line, '\n')) {
			boost::algorithm::split(parts, line, boost::is_any_of(" "));
			m_urls[parts[0]] = parts[1];
		}
		in.close();
	}
}

std::string VersionURLS::url(std::string version) {
	if (m_urls.count(version)) {
		return m_urls[version];
	}
	return "";
}

std::vector<std::string> VersionURLS::versions() {
	std::vector<std::string> versions;
	std::map<std::string, std::string>::iterator it;

	for (it=m_urls.begin() ; it != m_urls.end(); it++) {
		versions.push_back(it->first);
	}

	return versions;
}
